package com.spzx.product.controller;


import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.PageHelper;
import com.spzx.common.core.constant.SecurityConstants;
import com.spzx.common.core.domain.R;
import com.spzx.common.core.web.controller.BaseController;
import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.common.core.web.page.TableDataInfo;
import com.spzx.common.redis.cache.GuiguCache;
import com.spzx.common.security.annotation.InnerAuth;
import com.spzx.product.api.domain.vo.*;
import com.spzx.product.api.domain.vo.Product;
import com.spzx.product.mapper.ProductMapper;
import com.spzx.product.service.IProductService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/product")
@Tag(name = "商品管理")
public class  ProductController extends BaseController {
    @Autowired
    IProductService productService;
    @Resource
    ProductMapper productMapper;

    @InnerAuth
    @GetMapping("checkAndLock/{orderNo}")
    public R<String> checkAndLock(@PathVariable String orderNo, @RequestBody List<SkuLockVo> skuLockVoList){
        String notify = productService.checkAndLock(orderNo, skuLockVoList);
        return R.ok(notify);// 根据空字符串或者库存信息返回不同结果

    }

    @GetMapping(value = "/getSkuPriceList")
    @InnerAuth
    public R<List<SkuPrice>>  getSkuPriceList(@RequestBody List<Long> skuIdList, @RequestHeader(SecurityConstants.INNER)String inner){
        List<SkuPrice> skuPriceList = productService.getSkuPriceList(skuIdList);
        return R.ok(skuPriceList);
    }

    @GetMapping("getProductSku/{skuId}")
    @Operation(summary = "sku")
    @InnerAuth
    @GuiguCache(prefix = "productSku:")
    public R<ProductSku> getProductSkuById(@PathVariable("skuId")Long skuId){
        return R.ok(productService.getProductSku(skuId));
    }
    @GetMapping("getProduct/{id}")
    @Operation(summary = "product")
    @InnerAuth
    @GuiguCache(prefix = "product:")
    public R<Product> getProductById(@PathVariable("id")Long id){
        Product product = productMapper.selectById(Wrappers.lambdaQuery(Product.class).eq(Product::getId, id));
        return R.ok(product);
    }

    @GetMapping("getSkuPrice/{skuId}")
    @Operation(summary = "SkuPrice")
    @InnerAuth
    @GuiguCache(prefix = "SkuPrice:")
    public R<SkuPrice> getSkuPrice(@PathVariable("skuId") Long skuId){
        SkuPrice skuPrice = productService.getSkuPrice(skuId);
        return R.ok(skuPrice);
    }
    @Operation(summary = "获取商品详细信息")
    @InnerAuth
    @GetMapping(value = "/getProductDetails/{id}")
    @GuiguCache(prefix = "ProductDetails:")
    public R<ProductDetails> getProductDetails(@PathVariable("id") Long id)
    {
        ProductDetails productDetails = productService.getProductDetails(id);
        return R.ok(productDetails);
    }

    @Operation(summary = "获取商品sku规则详细信息")
    @InnerAuth
    @GetMapping(value = "/getSkuSpecValue/{id}")
    @GuiguCache(prefix = "SkuSpecValue:")
    public R<Map<String, Long>> getSkuSpecValue(@PathVariable("id") Long id)
    {
        Map<String, Long> specToSkuMap = productService.getSkuSpecValue(id);
        return R.ok(specToSkuMap);
    }

    @Operation(summary = "获取商品sku库存信息")
    @InnerAuth
    @GetMapping(value = "/getSkuStock/{skuId}")
    @GuiguCache(prefix = "SkuStock:")
    public R<SkuStockVo> getSkuStock(@PathVariable("skuId") Long skuId)
    {
        SkuStockVo skuStockVo = productService.getSkuStock(skuId);
        return R.ok(skuStockVo);
    }





    @Operation(summary = "获取销量好的sku")
    @GetMapping("getTopSale")
    public R<List<ProductSku>> getTopSale() {
        return R.ok(productService.getTopSale());
    }
    @Operation(summary = "查询商品列表")
    @GetMapping("list")
    public TableDataInfo list(Product product){
       startPage();
     List<Product>list = productService.selectProductList(product);
     return getDataTable(list);

    }

    @Operation(summary = "新增商品")
    @PostMapping
    public AjaxResult add (@RequestBody Product product){
        return toAjax(productService.insertProduct(product));
    }
    @Operation(summary = "获取商品详细信息")
    @GetMapping("{id}")
    public AjaxResult getInfo(@PathVariable("id")Long id){
        return success(productService.selectProductById(id));
    }
    @Operation(summary = "修改商品")
    @PutMapping
    public AjaxResult edit(@RequestBody Product product) {
        return toAjax(productService.updateProduct(product));
    }

    @Operation(summary = "删除商品")
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(productService.deleteProductByIds(ids));
    }

    @Operation(summary = "商品审核")
    @GetMapping("updateAuditStatus/{id}/{auditStatus}")
    public AjaxResult updateAuditStatus(@PathVariable Long id, @PathVariable Integer auditStatus) {
        productService.updateAuditStatus(id, auditStatus);
        return success();
    }

    @Operation(summary = "更新上下架状态")
    @GetMapping("updateStatus/{id}/{status}")
    public AjaxResult updateStatus(@PathVariable Long id, @PathVariable Integer status) {
        productService.updateStatus(id, status);
        return success();
    }

    @InnerAuth
    @GetMapping("/skuList/{pageNum}/{pageSize}")
    public R<TableDataInfo> skuList(
            @Parameter(name = "pageNum",description = "当前页码",required = true) @PathVariable Integer pageNum,
            @Parameter(name="pageSize",description = "每页记录数",required = true)@PathVariable Integer pageSize,
            @Parameter(name = "productQuery",description = "查询对象",required = true)@ModelAttribute SkuQuery skuQuery
            ){
        PageHelper.startPage(pageNum,pageSize);
        List<SkuQuery> list = productService.getProductSkuList(skuQuery);
        return R.ok(getDataTable(list));

    }


}
